/**
 *====================================================
 * 文件名称: FinanceProductExpenseServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年11月11日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.project.entity.FinanceProductExpenseEntity;
import com.yuexunit.finance.project.entity.lexicon.ExpenseTypeEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;

/**
 * @ClassName: FinanceProductExpenseServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年11月11日 17:09:55
 */
@Implements(FinanceProductExpenseService.class)
public class FinanceProductExpenseServiceImpl implements FinanceProductExpenseService {

	@Override
	public Long createFinanceProductExpense(FinanceProductExpenseEntity entity) {
		PersistUtil.insert(entity);
		return entity.getProductExpenseId();
	}

	@Override
	public void deleteFinanceProductExpense(Long productExpenseId) {
		PersistUtil.delete(FinanceProductExpenseEntity.class, productExpenseId);
	}

	@Override
	public List<FinanceProductExpenseEntity> inquireFinanceProductExpense(Long productId, ExpenseTypeEnum expenseType) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceProductExpenseEntity.class);
		criteria.add(Restrictions.eq("productId", productId));
		criteria.add(Restrictions.eq("expenseType", expenseType));
		return PersistUtil.get(criteria);
	}

	@Override
	public PageList<FinanceProductExpenseEntity> inquireFinanceProductExpensePageList(Long productId, ExpenseTypeEnum expenseType, String expenseName, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceProductExpenseEntity.class);
		criteria.add(Restrictions.eq("productId", productId));
		criteria.add(Restrictions.eq("expenseType", expenseType));
		if (StringUtils.isNotBlank(expenseName)) {
			criteria.add(Restrictions.like("expenseName", "%" + expenseName + "%"));
		}
		return PersistUtil.get(criteria, page);
	}

	@Override
	public FinanceProductExpenseEntity inquireFinanceProductExpense(Long productId, Long bulkstockCategoryId, Long bulkstockInfoId, Integer day, ExpenseTypeEnum expenseType) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceProductExpenseEntity.class);
		criteria.add(Restrictions.eq("productId", productId));
		criteria.add(Restrictions.eq("expenseType", expenseType));
		criteria.add(Restrictions.le("startDay", day));
		criteria.add(Restrictions.ge("endDay", day));
		criteria.add(Restrictions.or(Restrictions.eq("bulkstockCategoryId", bulkstockCategoryId), Restrictions.isNull("bulkstockCategoryId")));
		criteria.add(Restrictions.or(Restrictions.eq("bulkstockInfoId", bulkstockInfoId), Restrictions.isNull("bulkstockInfoId")));
		criteria.addOrder(Order.desc("bulkstockInfoId"));
		criteria.addOrder(Order.desc("bulkstockCategoryId"));
		criteria.addOrder(Order.desc("amount"));
		List<FinanceProductExpenseEntity> expenses = PersistUtil.get(criteria);
		if (expenses.isEmpty()) {
			return null;
		}
		return expenses.get(0);
	}

}
